import React, { useEffect, useState } from 'react'
import { useNavigate } from 'react-router-dom'
import E from 'wangeditor'
import request from '../../utils'
type Props = {}
let ed: any = null
const Index = (props: Props) => {
	const [title, setTitle] = useState('')
	const [price, setPrice] = useState('')
	const [desc, setDesc] = useState('')
	const [detail, setDetail] = useState('')
	const navigate = useNavigate()
	useEffect(() => {
		const editor = new E('#div1')
		editor.create()

		ed = editor
	}, [])

	const onSubmit = async () => {		
		const data = {
			title,
			price,
			desc,
			"detail": ed.txt.html(),
			key: Math.random().toString().slice(2)
		}

		const resp = await request({ url: '/api/list/add', method: 'post', data })

		const { code, mess } = resp.data

		if(code === 200) {
			navigate('/table/list')
		}
		
	}

	return (
		<div>
			<input type="text" placeholder='请输入标题' value={title} onChange={e => setTitle(e.target.value)} />
			<input type="text" placeholder='请输入单价' value={price} onChange={e => setPrice(e.target.value)} />
			<input type="text" placeholder='请输入描述' value={desc} onChange={e => setDesc(e.target.value)} />
			<h3>详情页</h3>
			<div id='div1'></div>
			<button onClick={onSubmit}>提交</button>
		</div>
	)
}

export default Index